{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/gee-community/geemap/blob/master/docs/notebooks/149_gemini.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "**Using Gemini and EE Genie for automatic code generation**\n",
    "\n",
    "The source code is adapted from the ee_genie.ipynb notebook at <https://bit.ly/3YEm7B6>.\n",
    "Credit to the original author [Simon Ilyushchenko](https://github.com/simonff).\n",
    "\n",
    "**Please note that this feature experimental. Using the Gemini API may incur charges.** Please checkout the pricing [here](https://ai.google.dev/pricing).\n",
    "\n",
    "\n",
    "## Installation\n",
    "\n",
    "Uncomment the following line to uninstall the existing geemap package. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip uninstall geemap -y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Uncomment the second line to install the development version of geemap."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install -U git+https://github.com/gee-community/geemap.git geedim"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pre-requisites\n",
    "\n",
    "To use this new feature, you need two things:\n",
    "1. Earth Engine access\n",
    "2. Generative AI API key\n",
    "\n",
    "You need a Google Cloud Project to associate your requests with. [Use these instructions](https://developers.google.com/earth-engine/cloud/earthengine_cloud_project_setup) and set `GOOGLE_PROJECT_ID` or `EE_PROJECT_ID` notebook secret to your project id.\n",
    "\n",
    "Next you need to get an Generative AI API key [here](https://aistudio.google.com/app/prompts/new_chat). \n",
    "\n",
    "\n",
    "To save this key in the notebook, click on the key icon in Colab on the left-hand side and add your key as a secret with the name `GOOGLE_API_KEY`. Make sure the value has no newlines.\n",
    "\n",
    "Alternatively, uncomment the following code block to set API keys if you have not set them as notebook secrets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import os\n",
    "# os.environ[\"EE_PROJECT_ID\"] = \"YOUR-PROJECT-ID\"\n",
    "# os.environ[\"GOOGLE_API_KEY\"] = \"YOUR-GOOGLE-API-KEY\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Use Genie"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from geemap.ai import Genie"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For a list of available Gemini models, see https://bit.ly/4fKfXW7."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Genie(\n",
    "    gemini_model=\"gemini-1.5-flash\",\n",
    "    target_score=0.8,\n",
    "    widget_height=\"600px\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/TPcG30t.jpeg)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
